using System;
using System.Linq;
using System.Threading.Tasks;
using HengTong.Model.Db.System;
using HengTong.ViewModel._Admin.SysNotificationVms;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WalkingTec.Mvvm.Core;

namespace HengTong.Areas._Admin.Controllers;

public partial class SysNotificationController
{
    /// <summary>
    /// 标记所有消息为已读
    /// </summary>
    /// <returns></returns>
    [AllRights]
    [ActionDescription("标记所有消息为已读")]
    [HttpPost("MarkAllAsRead")]
    public async Task<IActionResult> MarkAllAsRead()
    {
        var count = await DC.Set<SysNotification>()
            .Where(e => e.Receiver == Wtm.LoginUserInfo.ITCode)
            .Where(e => e.Status == NotificationStatusEnum.未读)
            .ExecuteUpdateAsync(e => e
                .SetProperty(x => x.Status, NotificationStatusEnum.已读 )
                .SetProperty(x => x.ReadTime, DateTime.Now));
        return Ok(count);
    }
    
    [ActionDescription("Sys.Get")]
    [HttpGet("{id}")]
    public SysNotificationVm Get(string id)
    {
        var vm = Wtm.CreateVM<SysNotificationVm>(id);
        if (vm?.Entity.Status == NotificationStatusEnum.未读)
        {
            vm.Entity.Status = NotificationStatusEnum.已读;
            vm.Entity.ReadTime = DateTime.Now;
            DC.UpdateEntity(vm.Entity);
            DC.SaveChanges();
        }
        return vm;
    }
}